package com.yby.dome7;

public class Test {
    public void instanceOf(People people){
        if (people instanceof Student){
            Student student = (Student)people;

        }
        if(people instanceof Worker){
            Worker worker = (Worker)people;
        }
    }
    public static void main(String[] args) {
        //向上转型
/*        People people = new Student();
        people.name = "小明";
        people.age = 20;
        people.profession = "学生";
        people.say();
        people = new Worker();
        people.name = "王五";
        people.age = 28;
        people.profession = "工人";
        people.say();*/

        // 先判断，再向下转型
        Test test = new Test();
        People people = new Student();
        People people1 = new Worker();
        test.instanceOf(people);
        people.name = "小红";
        people.age = 18;
        people.profession = "学生";
        people.say();
        test.instanceOf(people1);
        people1 = new Worker();
        people1.name = "张三";
        people1.age = 38;
        people1.profession = "工人";
        people1.say();


    }
}
